<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_freehandDrawFeatures"></title>
    <script type="text/javascript" src="../../dist/openlayers/include-openlayers.js"></script>
    <style>
        .ol-popup {
            position: absolute;
            top: 50px;
            right: 20px;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="width: 100%;height:100%"></div>
<div id="popup" class="ol-popup">
    <div class="btn-group" role="group" aria-label="...">
        <button id="drawLine" value='LineString' type="button" class="btn btn-default" data-i18n="resources.text_input_value_drawLine"></button>
        <button id="drawPolygon" value='Polygon' type="button" class="btn btn-default" data-i18n="resources.text_input_value_drawPolygon"></button>
        <button id="drawCircle" value='Circle' type="button" class="btn btn-default" data-i18n="resources.btn_drawCircle"></button>
        <button id="none" value='None' type="button" class="btn btn-default" data-i18n="resources.btn_notDraw"></button>
        <button id="clear" value='Clear' type="button" class="btn btn-default" data-i18n="resources.text_input_value_clear"></button>
    </div>
</div>
<script type="text/javascript" include="bootstrap,jquery" src="../js/include-web.js"></script>
<script type="text/javascript">
    //按住左键不放随意绘制图形，放手绘制完成
    var map, draw,
        url = (window.isLocal ? window.server : "http://support.supermap.com.cn:8090") + "/iserver/services/map-china400/rest/maps/China";
    map = new ol.Map({
        target: 'map',
        controls: ol.control.defaults({attributionOptions: {collapsed: false}})
            .extend([new ol.supermap.control.Logo()]),
        view: new ol.View({
            center: [12957388, 4853991],
            zoom: 4,
            projection: 'EPSG:3857'
        })
    });
    var layer = new ol.layer.Tile({
        source: new ol.source.TileSuperMapRest({
            url: url,
            wrapX: true
        }),
        projection: 'EPSG:3857'
    });
    var source = new ol.source.Vector({wrapX: false});
    var vector = new ol.layer.Vector({
        source: source
    });
    map.addLayer(layer);
    map.addLayer(vector);
    var info = new ol.control.Control({element: document.getElementById('popup')});
    info.setMap(map);
    map.addControl(info);

    var buttons = $('.btn-group').children();
    buttons.map(function (key) {
        var value = buttons[key].value;
        if (value === 'None') {
            $(buttons[key]).on('click', function () {
                clearInteraction();
            });
            return;
        }
        if (value === 'Clear') {
            $(buttons[key]).on('click', function () {
                clearInteraction();
                source.clear();
            });
            return;
        }
        $(buttons[key]).on('click', function () {
            clearInteraction();
            draw = new ol.interaction.Draw({
                source: source,
                type: buttons[key].value,
                snapTolerance: 20,
                freehand: true
            });
            map.addInteraction(draw);
        });
    });

    function clearInteraction() {
        if (draw) {
            map.removeInteraction(draw);
        }
    }

</script>
</body>
</html>